# coding:utf-8

from wtforms import StringField, SelectMultipleField, SubmitField
from wtforms_components import SelectField, DateField
from wtforms.validators import DataRequired, ValidationError, Optional

from app.auth.models import AdminUser
from app.forms import WphForm, NonValidatingSelectField, NotChoices
from .models import WarZone


class WarZoneDetailForm(WphForm):
    """
    created by chenhj at 2017/12/15

    """
    admin_id = SelectField('战区经理', validators=[DataRequired()], coerce=int)
    name = StringField('战区名', validators=[DataRequired()])
    area_ids = SelectMultipleField('省份', validators=[DataRequired()])
    submit = SubmitField('提交')

    def validate_admin_id(self, field):
        """
        created by chenhj at 15/12/2017

        """
        if field.data == -1:
            raise ValidationError('请选择战区经理')

    def __init__(self, *args, **kwargs):
        super(WarZoneDetailForm, self).__init__(*args, **kwargs)

        users = AdminUser.query.filter_by(
            deleted_at=0,
            is_administrator=True
        )
        self.admin_id.choices = NotChoices + \
            [
                (user.id, user.nickname)
                for user in users
            ]
        self.admin_id.default = NotChoices[0][0]


class SearchWarZoneStatForm(WphForm):
    """
    created by chenhj at 22/12/2017

    """
    war_zone_id = NonValidatingSelectField('战区', validators=[Optional()], coerce=int)
    date_begin = DateField('开始时间', validators=[Optional()])
    date_end = DateField('结束时间', validators=[Optional()])

    def __init__(self, *args, **kwargs):
        super(SearchWarZoneStatForm, self).__init__(*args, **kwargs)

        zones = WarZone.query.all()
        self.war_zone_id.choices = NotChoices + \
            [
                (zone.id, zone.name)
                for zone in zones
            ]
        self.war_zone_id.default = NotChoices[0][0]
